singleApp.controller('dashboardCtrl', ['$http', '$scope', function ($http, $scope) {
    $scope.model_total = { recipe: 0, users: 0 };
    $scope.model_loading = { recipe: false, users: false };

    $scope.checkLoading = function () {
        if ($scope.model_loading.recipe == true && $scope.model_loading.users == true) {
            layer.closeAll();
        }
    };

    $scope.init = function () {
        var api_url = window.singleConfig.rootUrl + "api/admin/RecipeService/report_by_status";
        layer.load(2);

        $http.get(api_url).then(function (result) {
            var data = result.data;
            var count_disable = parseInt(data.data.count_disable);
            var count_visible = parseInt(data.data.count_visible);
            $scope.model_total.recipe = count_disable + count_visible;

            var recipeReport = c3.generate({
                data: {
                    // iris data from R
                    columns: [
                        [stringFormat("Disable( {0} )", count_disable), count_disable],
                        [stringFormat("Visible( {0} )", count_visible), count_visible],
                    ],
                    type: 'pie',
                },
                bindto: "#divRecipeReport",
            });
            $scope.model_loading.recipe = true;
            $scope.checkLoading();
        }, function () { });

        api_url = window.singleConfig.rootUrl + "api/admin/UsersService/report_by_from";
        $http.get(api_url).then(function (result) {
            var data = result.data;
            var count_admin = parseInt(data.data.count_admin);
            var count_createbyadmin = parseInt(data.data.count_createbyadmin);
            var count_register = parseInt(data.data.count_register);
            var count_loginbysocial = parseInt(data.data.count_loginbysocial);
            $scope.model_total.users = count_admin + count_createbyadmin + count_register + count_loginbysocial;

            var recipeReport = c3.generate({
                data: {
                    // iris data from R
                    columns: [
                        [stringFormat("Admin( {0} )", count_admin), count_admin],
                        [stringFormat("CreateByAdmin( {0} )", count_createbyadmin), count_createbyadmin],
                        [stringFormat("Register( {0} )", count_register), count_register],
                        [stringFormat("Login by Social( {0} )", count_loginbysocial), count_loginbysocial],
                    ],
                    type: 'pie',
                },
                bindto: "#divUsersReport",
            });

            $scope.model_loading.users = true;
            $scope.checkLoading();
        }, function () { });
    };

    $scope.init();
}]);